package com.bskj.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 短信服务工具类
 *
 * @author lishi
 * @date 2018年1月11日 上午9:41:00
 */
public class SmsUtils {
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    public static SendSmsResponse sendRegisterCode(String code, String phoneNum) throws Exception {
        String defaultConnectTimeout = Global.getConfig("sun.net.client.defaultConnectTimeout", "10000");
        String defaultReadTimeout = Global.getConfig("sun.net.client.defaultReadTimeout", "10000");
        String accessKeyId = Global.getConfig("accessKeyId", "LTAIheWKo0JVVGPs");
        String accessKeySecret = Global.getConfig("accessKeySecret", "0126GK59FoawsrRs1t6sm2OXs9aVSl");
        String signName = Global.getConfig("signName", "泊数科技");
        LogUtils.debug("签名:" + signName);
        String sendMsgTemplateCode = Global.getConfig("sendMsgTemplateCode", "SMS_117527433");

        if (StringUtils.isBank(accessKeyId) || StringUtils.isBank(accessKeySecret)) {
            throw new Exception("property accessKeyId,accessKeySecret,signName is required in config/conf.properties！");
        }

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", defaultConnectTimeout);
        System.setProperty("sun.net.client.defaultReadTimeout", defaultReadTimeout);

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phoneNum);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(sendMsgTemplateCode);
        JSONObject paramObj = new JSONObject();
        paramObj.put("code", code);
        request.setTemplateParam(paramObj.toJSONString());

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //		request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        LogUtils.debug(JSONObject.toJSONString(sendSmsResponse));
        return sendSmsResponse;
    }

    /**
     * 随机生成6位随机验证码 方法说明
     *
     * @return String
     * @Discription:扩展说明
     * @Author: feizi
     * @Date: 2015年4月17日 下午7:19:02
     * @ModifyUser：feizi
     * @ModifyDate: 2015年4月17日 下午7:19:02
     */
    public static String createRandomVcode() {
        // 验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 9);
        }
        return vcode;
    }

    public static void main(String[] args) throws Exception {
        SendSmsResponse sendSmsResponse = sendRegisterCode("520520", "13077338001");
        System.out.println(sendSmsResponse.getCode());
    }
}
